/*
 * @Author: your name
 * @Date: 2021-06-25 14:46:45
 * @LastEditTime: 2021-06-27 16:03:19
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_shop\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from './../components/Login'

import Home from './../components/Home'


Vue.use(VueRouter)

const routes = [
    // 路由重定向
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/home',
        component: Home
    }
]

const router = new VueRouter({
    routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to将要访问的路径
    //from 从那个路径跳转而来
    //next是个函数，表示放行
    if (to.path === '/login') return next()

    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router